package com.lc.cehui.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.dialog.AddressDialog;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.CompanyChildTypeApi;
import com.lc.cehui.http.response.CompanyChildtypeBean;
import com.lc.cehui.http.response.CompanyTypeBean;
import com.lc.cehui.ui.activity.home.ResumeDetailsActivity;
import com.lc.cehui.ui.adapter.ChoiceAdapter;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 人才列表
 */
public class PersonnelListActivity extends AppActivity implements
        TextView.OnEditorActionListener{
    private static final String TAG = "PersonnelListActivity";
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.ll_seach)
    LinearLayout llSeach;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    @BindView(R.id.ll_choice)
    LinearLayout llChoice;
    @BindView(R.id.tv_gd)
    TextView tvGd;
    @BindView(R.id.tv_qy)
    TextView tvQy;
    @BindView(R.id.tv_jy)
    TextView tvJy;
    @BindView(R.id.tv_xz)
    TextView tvXz;
    @BindView(R.id.recyclerView_choice)
    RecyclerView recyclerViewChoice;
    @BindView(R.id.et_text)
    EditText etText;
    private BaseQuickAdapter adapter;
    private ChoiceAdapter choiceAdapter;
    private String choiceType = "";
    private int mPage;
    private  List<CompanyTypeBean.DataBean> typelist = new ArrayList<>();
    private List<CompanyTypeBean.DataBean.List2Bean> list=new ArrayList<>();
    private String id;
    private String Type="";
    private String years="";
    private String salary="";
    private String education="";
    private String key="";
    /**
     * 省
     */
    private String mProvince = "";
    /**
     * 市
     */
    private String mCity = "";
    /**
     * 区
     */
    private String mArea = "";
    private List<CompanyChildtypeBean.DataBean> datalist=new ArrayList<>();
    private String flag;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personnel_list;
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra("id");
        company_childtype(mPage, id,key,mProvince,mCity,mArea,years,salary,education);
        company_type();
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                Log.e("TAG", "onRefresh:---- " );
                super.onRefresh(refreshLayout);
//                new Handler().postDelayed(() -> {

                mPage=1;
                datalist.clear();
                company_childtype(mPage,id,key,mProvince,mCity,mArea,years,salary,education);
                refreshLayout.finishRefreshing();
//                }, 1000);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);



                if (flag.equals("T")) {
                    mPage++;
                    company_childtype(mPage,id,key,mProvince,mCity,mArea,years,salary,education);

                }
                refreshLayout.finishLoadmore();

            }
        });

        etText.setOnEditorActionListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter = new BaseQuickAdapter<CompanyChildtypeBean.DataBean, BaseViewHolder>(R.layout.adapter_personnellist,datalist) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, CompanyChildtypeBean.DataBean bean) {
                GlideApp.with(PersonnelListActivity.this).load(bean.getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) baseViewHolder.getView(R.id.im_logo));
                baseViewHolder.setText(R.id.tv_title, bean.getTitle());
                baseViewHolder.setText(R.id.tv_body, bean.getWork_city()+"  ｜ "+bean.getYears()+"  ｜ "+bean.getEducation());
                baseViewHolder.setText(R.id.tv_work, bean.getNickname());
                baseViewHolder.setText(R.id.tv_count, bean.getSalary());
            }
        });
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                startActivity(new Intent(PersonnelListActivity.this, ResumeDetailsActivity.class).putExtra("id",datalist.get(position).getJob_id()+""));
            }
        });

        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getContext());
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        recyclerViewChoice.setLayoutManager(layoutManager);

        choiceAdapter = new ChoiceAdapter(R.layout.adapter_choice_item, null);
        recyclerViewChoice.setAdapter(choiceAdapter);
        choiceAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                choiceType = choiceAdapter.getData().get(position).getId();
                if (Type.equals("经验")){
                    years =choiceAdapter.getData().get(position).getId();
                }else if (Type.equals("薪资")){
                    salary=choiceAdapter.getData().get(position).getId();
                }else if (Type.equals("学历")){
                    education=choiceAdapter.getData().get(position).getId();
                }
                mPage=1;
                datalist.clear();
                company_childtype(mPage,id,key,mProvince,mCity,mArea,years,salary,education);
                llChoice.setVisibility(View.GONE);
            }
        });
         showEmptyView(adapter);
    }

    private void company_childtype(int mPage,String id,String key,String province,String city,String district,String years,String salary,String education) {
        EasyHttp.post(this)
                .api(new CompanyChildTypeApi().setPage(mPage).setId(id).setKey(key).setProvince(province).setCity(city).setDistrict(district).setYears(years).setSalary(salary).setEducation(education))
                .request(new HttpCallback<CompanyChildtypeBean>(this) {

                    @Override
                    public void onSucceed(CompanyChildtypeBean data) {

                        if (data.getCode().equals("200")) {
                            flag = data.getFlag();
                            datalist.addAll(data.getData());
                            adapter.setNewData(datalist);
                            adapter.notifyDataSetChanged();


                        }

                    }
                });
    }
    private void company_type() {

        EasyHttp.post(this)
                .api("api/job_type")
                .request(new HttpCallback<CompanyTypeBean>(this) {

                    @Override
                    public void onSucceed(CompanyTypeBean data) {
                        typelist.addAll(data.getData());


                    }
                });
    }
    @Override
    protected void initData() {

    }

    @OnClick({R.id.ll_seach, R.id.tv_gd, R.id.tv_qy, R.id.tv_jy, R.id.tv_xz})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_seach:
                break;
            case R.id.tv_qy:
                setUI(tvQy, 0);
                break;
            case R.id.tv_jy:
                Type="经验";
                setUI(tvJy, 1);
                break;

            case R.id.tv_xz:
                Type="薪资";
                setUI(tvXz, 2);
                break;
            case R.id.tv_gd:

                Type="学历";
                setUI(tvGd, 3);
                break;
        }
    }

    private void setUI(TextView textView, int type) {
        switch (type) {
            case 0:
                new AddressDialog.Builder(this)
                        .setTitle("选择地区")
                        // 设置默认省份
//                        .setProvince(mProvince)
                        // 设置默认城市（必须要先设置默认省份）
//                        .setCity(mCity)
                        // 不选择县级区域
                        //.setIgnoreArea()
                        .setListener((dialog, province, city, area) -> {

                            if (!tvQy.getText().toString().equals(city)) {
                                mProvince = province;
                                mCity = city;
                                mArea = area;
                                tvQy.setText(mCity);
                                llChoice.setVisibility(View.GONE);
                                mPage=1;
                                datalist.clear();
                                company_childtype(mPage,id,key,mProvince,mCity,mArea,years,salary,education);
                            }
                        })
                        .show();

                break;
            case 1:
                choiceAdapter.setNewInstance(lists("工作年限"));
                break;
            case 2:
                choiceAdapter.setNewInstance(lists("薪资范围"));
                break;
            case 3:
                choiceAdapter.setNewInstance(lists("最低学历"));
                break;
        }
        choiceAdapter.notifyDataSetChanged();
        llChoice.setVisibility(View.VISIBLE);
        tvGd.setTextColor(getResources().getColor(R.color.color333333));
        tvQy.setTextColor(getResources().getColor(R.color.color333333));
        tvJy.setTextColor(getResources().getColor(R.color.color333333));
        tvXz.setTextColor(getResources().getColor(R.color.color333333));

        textView.setTextColor(getResources().getColor(R.color.blue168));
    }



    private List<CompanyTypeBean.DataBean.List2Bean> lists(String s) {
            list.clear();
        boolean isChecked = false;
        for (int i = 0; i < typelist.size(); i++) {
            Log.e(TAG, "lists: "+typelist.get(i).getTitle() );
            if (typelist.get(i).getTitle().equals(s)) {
                list.addAll(typelist.get(i).getList2());
            }
        }
        for (int i = 0; i <list.size() ; i++) {

            if (choiceType.equals(list.get(i).getId())){
                isChecked = true;
                list.get(i).setSelect(true);
            } else {
                list.get(i).setSelect(false);
            }


        }




        CompanyTypeBean.DataBean.List2Bean bean = new CompanyTypeBean.DataBean.List2Bean();
        bean.setTitle("不限");
        if (!isChecked){
            bean.setSelect(true);
        }
        list.add(bean);

        return list;
    }

    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // 模拟点击登录按钮
            key=etText.getText().toString().trim();
            mPage=1;
            datalist.clear();
            company_childtype(mPage,id,key,mProvince,mCity,mArea,years,salary,education);
            hideKeyboard(getCurrentFocus());
//            搜索


        }
        return false;
    }
}